package BaseDeDatos;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import ClasesBasicas.Sensor;

public class ConsultasSensor {
	
	
	public static List<Sensor> getSensores() {
		List<Sensor> sensores = new ArrayList<Sensor>();
		Sensor actual = new Sensor();
		Connection conn;
		String url;
		ResultSet rs;
		Statement stat;
		try {
			Class.forName("org.sqlite.JDBC");
			url="jdbc:sqlite:Funcionalidad.db";
			conn = DriverManager.getConnection(url,"","");
		    stat = conn.createStatement();
			rs = stat.executeQuery("select * from Sensor;");
			while (rs.next()) {
				actual = new Sensor();
				actual.setId_sensor(rs.getString("Id_Sensor"));
				actual.setDescripcion(rs.getString("Descripcion"));
				actual.setEstado(rs.getString("Estado"));
				sensores.add(actual);
			}
			conn.close();
			stat.close();
			rs.close();
			return sensores;
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return new ArrayList <Sensor>();
		}

		

	}
	
	public static Sensor getSensor(String Id_Sensor) {
		
		Sensor sensor = new Sensor();
		Connection conn;
		PreparedStatement ps ;
		ResultSet rs;
		String url;
		try {
			Class.forName("org.sqlite.JDBC");
			url="jdbc:sqlite:Funcionalidad.db";
			conn = DriverManager.getConnection(url,"","");
			ps = conn.prepareStatement("select * from Sensor where Id_Sensor = ? ;");
			ps.setString(1, Id_Sensor);
			rs=ps.executeQuery();
			while (rs.next()) {
				sensor.setId_sensor(rs.getString("Id_Sensor"));
				sensor.setDescripcion(rs.getString("Descripcion"));
				sensor.setEstado(rs.getString("Estado"));
			}
			conn.close();
			rs.close();
			return sensor;
		} catch (Exception e) {
			System.out.println("Error al obtener el sensor "+Id_Sensor);
			return new Sensor();
		}
}
public static void encenderSensor(String Id_Sensor) {
		
	Connection conn;
	PreparedStatement ps ;
	String url;
	try {
		Class.forName("org.sqlite.JDBC");
		url="jdbc:sqlite:Funcionalidad.db";
		conn = DriverManager.getConnection(url,"","");
			ps = conn.prepareStatement("update Sensor set Estado='ON' where Id_Sensor = ? ;");
			ps.setString(1, Id_Sensor);
			ps.executeUpdate();
			conn.close();
			ps.close();
		} catch (Exception e) {
			System.out.println("Error al encender el sensor "+Id_Sensor);
			e.printStackTrace();
		}
}

public static void apagarSensor(String Id_Sensor) {
		
	Connection conn;
	PreparedStatement ps ;
	String url;
	try {
		Class.forName("org.sqlite.JDBC");
		url="jdbc:sqlite:Funcionalidad.db";
		conn = DriverManager.getConnection(url,"","");
			ps = conn.prepareStatement("update Sensor set Estado='OFF' where Id_Sensor = ? ;");
			ps.setString(1, Id_Sensor);
			ps.executeUpdate();
			conn.close();
			ps.close();
		} catch (Exception e) {
			System.out.println("Error al apagar el sensor "+Id_Sensor);
			e.printStackTrace();
		}
}

	
	public static void main(String [] args)
	{
		List<Sensor> sensores=ConsultasSensor.getSensores();
		System.out.println(sensores.size());
		for(int i=0; i<sensores.size();i++)
		{
			System.out.println(sensores.get(i).getId_sensor());
		}
	}

}
